<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>优惠卷管理</el-breadcrumb-item>
      <el-breadcrumb-item>优惠卷列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getCouponList">
            <el-button slot="append" icon="el-icon-search" @click="getCouponList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="AdddialogVisible = true">添加优惠卷</el-button>
          <el-button type="danger" @click="delGroup" :disabled="this.sels.length === 0">批量删除</el-button>
        </el-col>
      </el-row>

      <el-table :data="couponsList" border stripe @selection-change="selsChange">
        <el-table-column type="selection" width="55"></el-table-column>

        <el-table-column label="名称" prop="cp_name"></el-table-column>
        <el-table-column label="发放数量" prop="cp_num"></el-table-column>

        <el-table-column label="已领取" prop="cp_taken_num"></el-table-column>

        <el-table-column label="满" prop="goods_total_price"></el-table-column>

        <el-table-column label="减" prop="minus_price"></el-table-column>
        <el-table-column>
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEdit(scope.row.id)"
            ></el-button>

            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="remove(scope.row.id)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1,2,5,10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加 -->
    <el-dialog title="添加优惠卷" :visible.sync="AdddialogVisible" width="50%" @close="AddDialogClosed">
      <el-form :model="Addcoupon" :rules="couponRules" ref="couponRef" label-width="80px">
        <el-form-item label="名称" prop="cp_name">
          <el-input v-model="Addcoupon.cp_name"></el-input>
        </el-form-item>
        <el-form-item label="发放数量" prop="cp_num">
          <el-input v-model="Addcoupon.cp_num" type="number"></el-input>
        </el-form-item>
        <el-form-item label="满" prop="goods_total_price">
          <el-input v-model="Addcoupon.goods_total_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="减" prop="minus_price">
          <el-input v-model="Addcoupon.minus_price" type="number"></el-input>
        </el-form-item>

        <el-form-item label="开始时间" prop="start_date" width="220px">
          <el-date-picker
            v-model="Addcoupon.start_date"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="end_date" width="220px">
          <el-date-picker
            v-model="Addcoupon.end_date"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AdddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="修改优惠卷" :visible.sync="dialogVisible" width="50%" @close="DialogClosed">
      <el-form :model="coupon" :rules="couponRules" ref="couponRef" label-width="80px">
        <el-form-item label="名称" prop="cp_name">
          <el-input v-model="coupon.cp_name"></el-input>
        </el-form-item>
        <el-form-item label="发放数量" prop="cp_num">
          <el-input v-model="coupon.cp_num" type="number"></el-input>
        </el-form-item>
        <el-form-item label="满" prop="goods_total_price">
          <el-input v-model="coupon.goods_total_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="减" prop="minus_price">
          <el-input v-model="coupon.minus_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="期限">{{this.start_date|dateFormat}} 至 {{this.end_date|dateFormat}}</el-form-item>
        <el-form-item label="开始时间" prop="start_date" width="220px">
          <el-date-picker v-model="coupon.start_date" type="datetime" value-format="timestamp"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="end_date" width="220px">
          <el-date-picker
            v-model="coupon.end_date"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit(coupon.id)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { server } from '../api'
export default {
  data() {
    return {
      sels: [], //选中的值显示
      //获取用户列表参数对象
      queryInfo: {
        query: '',
        //当前页数
        pagenum: 1,
        //当前页显示多少条

        pagesize: 10
      },
      dialogVisible: false,
      total: 0,
      couponsList: [],
      coupon: [],
      start_date: 0,
      end_date: 0,
      Addcoupon: {
        cp_name: '',
        cp_num: 0,
        goods_total_price: 0,
        minus_price: 0,
        start_date: 0,
        end_date: 0
      },
      AdddialogVisible: false,
      couponRules: {
        cp_name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        cp_num: [
          { required: true, message: '请输入发放数量', trigger: 'blur' }
        ],
        goods_total_price: [
          { required: true, message: '请输入满多少', trigger: 'blur' }
        ],
        minus_price: [
          { required: true, message: '请输入减免多少', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCouponList()
  },
  methods: {
    async getCouponList() {
      const { data: res } = await server.getCoupons(this.queryInfo)
      if (res.code !== 200) {
        return this.$message.error('获取失败')
      }
      this.couponsList = res.data.data
      this.total = res.data.total
    },
    //监听分页改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCouponList()
    },
    //监听页码值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getCouponList()
    },

    async showEdit(id) {
      const { data: res } = await server.detailCoupons(id)

      if (res.code !== 200) {
        return this.$message.error('查询失败！')
      }
      this.coupon = res.data
      this.start_date = this.coupon.start_date
      this.end_date = this.coupon.end_date
      console.log(this.coupon.start_date)
      this.dialogVisible = true

      console.log(res)
    },
    //关闭事件
    DialogClosed() {
      this.$refs.couponRef.resetFields()
    },
    AddDialogClosed() {
      this.$refs.couponRef.resetFields()
    },
    edit(id) {
      this.$refs.couponRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项')
        }
        var start = this.coupon.start_date.toString()
        var start = start.slice(0, 10)
        this.coupon.start_date = this.start_date = parseInt(start)
        var end = this.coupon.end_date.toString()
        var end = end.slice(0, 10)
        this.coupon.end_date = this.end_date = parseInt(end)
        const { data: res } = await server.editCoupons(id, this.coupon)
        if (res.code !== 200) {
          return this.$message.error('修改失败')
        }
        this.getCouponList()
        this.$message.success('修改成功')
        this.dialogVisible = false
      })
    },
    add() {
      this.$refs.couponRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项')
        }
        var start = this.Addcoupon.start_date.toString()
        var start = start.slice(0, 10)
        this.Addcoupon.start_date = parseInt(start)
        var end = this.Addcoupon.end_date.toString()
        var end = end.slice(0, 10)
        this.Addcoupon.end_date = parseInt(end)
        const { data: res } = await server.addCoupons(this.Addcoupon)
        if (res.code !== 200) {
          return this.$message.error('添加失败')
        }
        this.$message.success('添加成功！')
        this.AdddialogVisible = false
        this.getCouponList()
      })
    },
    remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async delete(id) {
      const { data: res } = await server.delCoupons(id)

      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      //刷新列表
      this.getCouponList()
      this.$message.success('删除成功！')
    },
    selsChange(sels) {
      this.sels = sels
    },
    delGroup() {
      var ids = this.sels.map(item => item.id).join() //获取所有选中行的id组成的字符串，以逗号分隔

      this.$confirm('此操作将永久删除选中文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delAll(ids)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async delAll(ids) {
      console.log(ids)
      const { data: res } = await server.delAllCoupons(ids)

      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      //刷新列表
      this.getCouponList()
      this.$message.success('删除成功！')
    }
  }
}
</script>

<style scoped>
</style>